#   LibAut
#       Figure 2 
#   Matthew C. Wilson, Juraj Medzihorsky
#   2021-08-10

#   sessionInfo()
##  R version 3.6.3 (2020-02-29)
##  Platform: x86_64-pc-linux-gnu (64-bit)
##  Running under: Ubuntu 16.04.7 LTS
##
##  Matrix products: default
##  BLAS/LAPACK: /opt/OpenBLAS/lib/libopenblas_haswellp-r0.3.9.so
##
##  locale:
##   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
##   [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C
##   [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
##  attached base packages:
##  [1] stats     graphics  grDevices utils     datasets  methods   base
##
##  other attached packages:
##  [1] nvimcom_0.9-28 colorout_1.2-0
##
##  loaded via a namespace (and not attached):
##  [1] compiler_3.6.3 tools_3.6.3

code_dir <- getwd()
figs_dir <- gsub('code$', 'figures', code_dir)

setwd(figs_dir)

#------------------------------------------------------------------------------
#   fake data

X <- c(1.00000, 3.85859, 6.71717, 9.57576, 12.43434, 15.29293, 18.15152,
       21.01010, 23.86869, 26.72727, 29.58586, 32.44444, 35.30303, 38.16162,
       41.02020, 43.87879, 46.73737, 49.59596, 52.45455, 55.31313, 58.17172,
       61.03030, 63.88889, 66.74747, 69.60606, 72.46465, 75.32323, 78.18182,
       81.04040, 83.89899, 86.75758, 89.61616, 92.47475, 95.33333, 98.19192,
       101.05051, 103.90909, 106.76768, 109.62626, 112.48485, 115.34343,
       118.20202, 121.06061, 123.91919, 126.77778, 129.63636, 132.49495,
       135.35354, 138.21212, 141.07071, 143.92929, 146.78788, 149.64646,
       152.50505, 155.36364, 158.22222, 161.08081, 163.93939, 166.79798,
       169.65657)
Y <- c(0.991940900, 0.990213200, 0.988119400, 0.985584200, 0.982517600,
       0.978812600, 0.974343000, 0.968960400, 0.962492100, 0.954738800,
       0.945473600, 0.934442000, 0.921364200, 0.905940100, 0.887858800,
       0.866812800, 0.842517600, 0.814737800, 0.783318000, 0.748216200,
       0.709536600, 0.667555100, 0.622732200, 0.575707000, 0.527269900,
       0.478313900, 0.429771000, 0.382542100, 0.337432100, 0.295099200,
       0.256025000, 0.220506200, 0.188665400, 0.160475800, 0.135793200,
       0.114389800, 0.095985170, 0.080273360, 0.066942970, 0.055692200,
       0.046238600, 0.038324590, 0.031720060, 0.026222660, 0.021656710,
       0.017871200, 0.014737420, 0.012146360, 0.010006220, 0.008240019,
       0.006783435, 0.005582881, 0.004593822, 0.003779318, 0.003108778,
       0.002556903, 0.002102790, 0.001729190, 0.001421872, 0.001169107)

Z1 <- c(c(-Y*.5)+.5)^1.3
Z2 <- c(c(-Y*.5)+.5)^1.3; for(z in 1:length(Z2)){Z2[z] <- Z2[z]*c(1-(z*.015))}
Z3 <- c(-Y+1); for(z in 1:length(Z3)){ Z3[z] <- Z3[z]*c(1-(z*.009))}
Z4 <- c(-Y+1)*.75

X <- (X - min(X))/dist(range(X))

#------------------------------------------------------------------------------
#   figure

xe <- c(X[1], X[60])
xlab <- c('start', 'end')
acex <- 1.00

ye <- c(0, 0.5, 1)

mg <- grey(0.0)
alwd <- 0.618

#   nature color palette
my_blue   <- rgb(86, 180, 233, max=255)
my_red    <- rgb(213, 94, 0, max=255)
my_orange <- rgb(230, 159, 0, max=255)

pdf("Figure_2.pdf", width=6.4, height=2.135); # 7/2.7, 6.6/2.56, 6.6/2.22,  
par(mfrow=c(1,4), mar=c(1.618,1.618,1.00,0.382), las=1, oma=c(0,1.382,0,0))
plot(Z2~X, type="l", ylim=c(0,1), col="white", lwd=2, 
     ylab="", xaxt="n",  yaxt="n", bty="n", 
     main="", cex.main=1.00, font.main=1,
     cex.axis=1.00, cex.lab=1.00)
    pu <- par('usr')
    text(0.5, pu[4], "Reverted liberalization\n ", pos=3, xpd=T, offset=-1)
    abline(h=.5, lty='22', lwd=0.618, col=grey(0.5), lend='round')
    axis(1, at=xe, labels=rep('', 2), lwd=alwd, cex.axis=acex, tck=-1.618*1e-2, col=mg)
    axis(1, at=xe, xlab, lwd=0, line=-0.618, cex.axis=acex)
    axis(2, at=ye, rep('',3), lwd=0.618, tck=-1.618*1e-2, cex.axis=acex)
    axis(2, at=ye, lwd=0, line=-0.5, cex.axis=acex)
    lines(X, Z2, lwd=2, col=my_orange)
title(ylab='Electoral Democracy', outer=T, xpd=T, line=0.382)
plot(Z1~X, type="l", ylim=c(0,1), col="white", lwd=2, 
     ylab="", xaxt="n", yaxt="n", bty="n", 
     main="", cex.main=1.00, font.main=1,
     cex.axis=1.00, cex.lab=1.00)
    text(0.5, pu[4], "Stabilized electoral\nautocracy", pos=3, xpd=T, offset=-1)
    abline(h=.5, lty='22', lwd=0.618, col=grey(0.5), lend='round')
    axis(1, at=xe, labels=rep('', 2), lwd=0.618, cex.axis=acex, tck=-1.618*1e-2)
    axis(1, at=xe, xlab, lwd=0, line=-0.618, cex.axis=acex)
    lines(X, Z1, lwd=2, col=my_orange)
plot(Z3~X, type="l", ylim=c(0,1), col="white", lwd=2, 
     ylab="", xaxt="n",  yaxt="n", bty="n", 
     main="", cex.main=1.00, font.main=1,
     cex.axis=1.00, cex.lab=1.00)
    text(0.5, pu[4], "Preempted democratic\ntransition", pos=3, xpd=T, offset=-1)
    abline(h=.5, lty='22', lwd=0.618, col=grey(0.5), lend='round')
    axis(1, at=xe, labels=rep('', 2), lwd=0.618, cex.axis=acex, tck=-1.618*1e-2)
    axis(1, at=xe, xlab, lwd=0, line=-0.618, cex.axis=acex)
    lines(X, Z3, lwd=2, col=my_orange)
plot(Z4~X, type="l", ylim=c(0,1), col="white", lwd=2, 
     ylab="", xaxt="n",  yaxt="n", bty="n", 
     main="", cex.main=1.00, font.main=1,
     cex.axis=1.00, cex.lab=1.00)
    text(0.5, pu[4], "Successful democratization\n ", pos=3, xpd=T, offset=-1)
    abline(h=.5, lty='22', lwd=0.618, col=grey(0.5), lend='round')
    axis(1, at=xe, labels=rep('', 2), lwd=0.618, cex.axis=acex, tck=-1.618*1e-2)
    axis(1, at=xe, xlab, lwd=0, line=-0.618, cex.axis=acex)
    lines(X, Z4, lwd=2, col=my_blue)
dev.off()

#   SCRIPT END